#include "widget.h"
#include "ui_widget.h"

#include <QDateTime>
#include <QDebug>

#include <QFile>
#include <QJsonObject>
#include <QJsonDocument>
#include <QJsonArray>
#include <QJsonParseError>
#include <QTableWidgetItem>

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
    QJsonObject jsonmain;
    jsonmain.insert("appname", QApplication::applicationName());
    jsonmain.insert("datetime", QDateTime::currentDateTime().toString());
    jsonmain.insert("version", 5);//QApplication.version
    jsonmain.insert("author", "vacant");
    bool flag = false;
    if(flag)
    {
        QJsonArray jsongroup;//分组
        for(int k = 0;k<2;k++)
        {//group
            QJsonObject jsongroup_sub;//分组
            jsongroup_sub.insert("name","group"+QString::number(1+k));// 参数
            jsongroup_sub.insert("ip","192.168.1.120");
            QJsonArray jsonplane;//该组对应餐数所投值-for
            {
                for(int j = 0;j<4;j++){//模拟4餐
                    QJsonObject jsoneach;//第?餐对应数据
                    {
                        jsoneach.insert("name",QString::fromLatin1("the %1 plane").arg(j+1));//餐数名称
                        jsoneach.insert("puttime",QDateTime::currentDateTime().toString());//投放时对应时间
                        QJsonArray jsarraybox;
                        for(int i=0;i<24;i++){
                            jsarraybox.append(QString::number(i+1));
                        }
                        jsoneach.insert("boxnum",jsarraybox);//箱号顺序
                        QJsonObject jsobjweigh;
                        for(int i=0;i<24;i++){
                            jsobjweigh.insert(QString::number(i+1),QString::number(i+12));
                        }
                        jsoneach.insert("weigh",jsobjweigh);//对应箱号-称量值
                        QJsonObject jsobjput;
                        for(int i=0;i<24;i++){
                            jsobjput.insert(QString::number(i+1),QString::number(i+5));
                        }
                        jsoneach.insert("putvalue",jsobjput);//对应箱号-抽喂值

                    }
                    jsonplane.append(jsoneach);//数组形式插入
                }
            }
            jsongroup_sub.insert("value",jsonplane);
            jsongroup.append(jsongroup_sub);
        }
        jsonmain.insert("value",jsongroup);
        QJsonDocument document;
        document.setObject(jsonmain);
        QByteArray byte_array = document.toJson(QJsonDocument::Indented);
        qDebug()<<"testJson:"<<byte_array;
        //    QString json_str(byte_array);

        //    doc.toBinaryData();
        QFile file("test.txt");
        file.fileName();
        if(file.open(QIODevice::WriteOnly)){
            qDebug("fileOpen success.");
            file.write(document.toJson());
        }else
            qDebug("fileOpen not success.");
    }

//    QByteArray data;
//    QFile file("test.txt");
//    file.fileName();
//    if(file.open(QIODevice::ReadOnly)){
//        qDebug("fileOpen success.");
//        data = file.readAll();
//        parseData(data);
//    }else
//        qDebug("fileOpen not success.");

    ui->tableWidget->setColumnCount(24);
    ui->tableWidget->insertRow(ui->tableWidget->rowCount());
    ui->tableWidget->setSpan(ui->tableWidget->rowCount()-1,0,ui->tableWidget->rowCount(),24);
    ui->tableWidget->insertRow(ui->tableWidget->rowCount());
    for(int i = 0;i<24;i++){
        QTableWidgetItem *item = new QTableWidgetItem;
        item->setText("new:"+QString::number(i+1));
        ui->tableWidget->setItem(ui->tableWidget->rowCount()-1,i,item);
    }
    ui->tableWidget->insertRow(ui->tableWidget->rowCount());

}

Widget::~Widget()
{
    delete ui;
}

QJsonObject Widget::parseMain(QByteArray byte_array,QJsonParseError *json_error)
{
//    QJsonParseError json_error;
    QJsonDocument parse_doucment = QJsonDocument::fromJson(byte_array, json_error);
    QJsonObject obj;
    if(json_error->error == QJsonParseError::NoError)
    {
        if(parse_doucment.isObject())
        {
            obj = parse_doucment.object();
        }
    }
    return obj;
}

QJsonObject Widget::parseGroup(QJsonObject obj,bool *ok)
{
    QJsonValue arr;
//    if(obj.contains("value")){
//        ok = obj.value("value").isArray();
//        return obj.value("value").toArray();
//    }
    return obj;
}


void Widget::parseData(QByteArray byte_array)
{
    //parse-解析
    {
        QJsonParseError json_error;
        QJsonObject obj = parseMain(byte_array,&json_error);

        qDebug()<<"json_error:"<<json_error.errorString();

        if(json_error.error == QJsonParseError::NoError){
            if(obj.contains("datetime"))
            {
                QJsonValue version_value = obj.take("datetime");
                if(version_value.isString())
                {
                    QString datatime = version_value.toVariant().toString();
                    qDebug()<<"datetime:"<<datatime;
                }
            }
            if(obj.contains("author"))
            {
                QJsonValue version_value = obj.take("author");
                if(version_value.isString())
                {
                    QString author = version_value.toString();
                    qDebug()<<"author:"<<author;
                }
            }
            if(obj.contains("value"))
            {
                foreach(QJsonValue value_group,obj.value("value").toArray())
                {
//                    //insert.
//                    //! [1]
//                    bool isExist = false;
//                    QVariantList list = obj.value("value").toArray.toVariantList();
//                    //check out the value exist.
//                    foreach(QVariant var ,list)
//                    {
//                        QRegExp reg("[\\d]{1,}");
//                        if(var.toString().contains(reg)){
////                            isExist = true;
//                            int index = list.indexOf(var);
//                            obj.value("value").toArray().replace(index,QJsonValue());
//                            break;
//                        }
//                    }
//                    //! [1]

                    qDebug()<<"group_sub array:";
                    QJsonObject obj_group = value_group.toObject();
                    QJsonValue _value = obj_group.take("ip");
                    if(_value.isString())
                    {
                        QString ipvalue = _value.toString();
                        qDebug()<<"ip:"<<ipvalue;
                    }
                    _value = obj_group.take("name");
                    if(_value.isString())
                    {
                        QString group_name = _value.toString();
                        qDebug()<<"name:"<<group_name;
//                        if(group_name)
                        //parse group data.
                        if(obj_group.contains("value"))
                        {
                            QJsonArray objarray_plane = obj_group.value("value").toArray();
                            if(!objarray_plane.isEmpty()){
                                int size_count = objarray_plane.size();
                                for(int j = 0;j<size_count;j++){
                                    QJsonValue value_plane = objarray_plane.at(j);
                                    if(value_plane.isObject()){
                                        QJsonObject value_plane_each = value_plane.toObject();
                                        if(value_plane_each.contains("boxnum")){
                                            qDebug()<<"boxnum";//<<value_plane_each.value("boxnum").toArray();
                                        }
                                        if(value_plane_each.contains("name")){
                                            qDebug()<<"name:"<<value_plane_each.value("name").toString();
                                        }
                                        if(value_plane_each.contains("puttime")){
                                            qDebug()<<"puttime:"<<value_plane_each.value("puttime").toString();
                                        }
                                        if(value_plane_each.contains("putvalue")){
                                            qDebug()<<"putvalue:";//<<value_plane_each.value("putvalue").toObject();
                                        }
                                        if(value_plane_each.contains("weigh")){
                                            qDebug()<<"weigh:";//<<value_plane_each.value("weigh").toObject();
                                        }
                                        qDebug()<<"value:"<<j;
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}
